package com.ysd.admin.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.impl.ChanPinXiangQingDaoimpl;
import com.ysd.dao.impl.FenLeiBiaoDaoimpl;
import com.ysd.entity.ChanPinXiangQing;
import com.ysd.entity.FenLeiBiao;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminChanPinFenLei extends BaseServlet {

	/**
	 * localhost:8080/xiyangyangchaoshi/adminchanpinfenlei?reqName=selChanPinFenLeiByPage
	 * 查询产品分类的详情
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void selChanPinFenLeiByPage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		FenLeiBiaoDaoimpl flb = new FenLeiBiaoDaoimpl();
		int page=Integer.parseInt(request.getParameter("page"));
		int size=Integer.parseInt(request.getParameter("size"));
		ArrayList<FenLeiBiao> flbList = flb.getFenLeiBiaoByPage(page,size);
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		Gson gson = new Gson();
		out.print(gson.toJson(flbList));

		out.flush();
		out.close();

	}
	/**
	 * localhost:8080/xiyangyangchaoshi/adminchanpinfenlei?reqName=selChanPinFenLei
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void selChanPinFenLei(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		FenLeiBiaoDaoimpl flb = new FenLeiBiaoDaoimpl();
		ArrayList<FenLeiBiao> flbList = flb.getFenLeiBiao();
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		Gson gson = new Gson();
		out.print(gson.toJson(flbList));

		out.flush();
		out.close();

	}
	
	
	/**
	 * localhost:8080/xiyangyangchaoshi/adminchanpinfenlei?reqName=delChanPingFenLei
	 * 根据产品分类id删除对应的数据
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delChanPingFenLei(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		FenLeiBiaoDaoimpl flb = new FenLeiBiaoDaoimpl();
		int fenLeiId = Integer.parseInt(request.getParameter("fenLeiId"));
		int n = flb.delChanPinFenLei(fenLeiId);
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		if (n > 0) {
			out.print(Result.toClient(true, "删除成功", ""));
		} else {
			out.print(Result.toClient(false, "删除失败", "请检查你提供的参数"));
		}

		out.flush();
		out.close();

	}

	/**
	 * localhost:8080/xiyangyangchaoshi/adminchanpinfenlei?reqName=selChanPinXiangQingId
	 * 根据分类查询数据
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void selChanPinXiangQingId(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ChanPinXiangQingDaoimpl cpxqDao = new ChanPinXiangQingDaoimpl();
		int chanPinFenLeiId = Integer.parseInt(request.getParameter("chanPinFenLeiId"));
		ArrayList<ChanPinXiangQing> cpxqId = cpxqDao.getChanPinXiangQingId(chanPinFenLeiId);
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		Gson gson = new Gson();
		out.print(gson.toJson(cpxqId));

		out.flush();
		out.close();

	}

	/**
	 * localhost:8080/xiyangyangchaoshi/adminchanpinfenlei?reqName=addChenPinFenLei
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addChenPinFenLei(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		FenLeiBiaoDaoimpl flbDao = new FenLeiBiaoDaoimpl();
		String leiMing = request.getParameter("leiMing");
		if (leiMing != null && !leiMing.equals("")) {
			ArrayList<FenLeiBiao> flbList = flbDao.getLeiMingByName(leiMing);
			if (flbList.size() == 0) {

				FenLeiBiao flb = new FenLeiBiao(0, leiMing);
				int n = flbDao.addChenPinFenLei(flb);
				if (n > 0) {
					out.print(Result.toClient(true, "添加成功", ""));
				} else {
					out.print(Result.toClient(false, "添加失败", "您的操作有误"));
				}

			} else {
				out.print(Result.toClient(false, "分类名已存在", ""));
			}
		}

		out.flush();
		out.close();

	}

	/**
	 * localhost:8080/xiyangyangchaoshi/adminchanpinfenlei?reqName=getFenLeiById
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getFenLeiById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		FenLeiBiaoDaoimpl flbDao = new FenLeiBiaoDaoimpl();
		int id = Integer.parseInt(request.getParameter("id"));
		FenLeiBiao flb = flbDao.getFenLeiById(id);

		Gson gson = new Gson();
		out.print(gson.toJson(flb));

		out.flush();
		out.close();

	}

	/**
	 * localhost:8080/xiyangyangchaoshi/adminchanpinfenlei?reqName=editChanPinFenLeiById
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void editChanPinFenLeiById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		FenLeiBiaoDaoimpl flbDao = new FenLeiBiaoDaoimpl();

		String chanPinFenLeiId = request.getParameter("chanPinFenLeiId");
		String leiMing = request.getParameter("chanPinFenLeiName");
		int chanPinId = 0;
		if (chanPinFenLeiId != null && !chanPinFenLeiId.equals("")) {
			chanPinId=Integer.parseInt(chanPinFenLeiId);
		}
		if (leiMing != null && !leiMing.equals("")) {
			ArrayList<FenLeiBiao> flbList = flbDao.getLeiMingByName(leiMing);
			if (flbList.size() == 0) {
				FenLeiBiao flb = new FenLeiBiao(chanPinId, leiMing);
				int n = flbDao.editChanPinFenLeiById(flb);
				if (n > 0) {
					out.print(Result.toClient(true, "修改成功", ""));
				} else {
					out.print(Result.toClient(false, "修改失败", "您的操作有误"));
				}
			} else {
				out.print(Result.toClient(false, "分类名已存在", ""));
			}
		}

		out.flush();
		out.close();

	}
	
	
	/**
	 * localhost:8080/xiyangyangchaoshi/adminchanpinfenlei?reqName=getFenLeiSize
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getFenLeiSize(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		FenLeiBiaoDaoimpl flbDao = new FenLeiBiaoDaoimpl();
		List<FenLeiBiao> flb= flbDao.getFenLeiSize();
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		Gson gson = new Gson();
		out.print(gson.toJson(flb.size()));

		out.flush();
		out.close();

	}

}
